package com.example.ggkt.user.controller;

import com.example.ggkt.common.BaseException;
import com.example.ggkt.user.domain.Student;
import com.example.ggkt.user.domain.StudentQuery;
import com.example.ggkt.user.mapper.StudentMapper;
import com.example.ggkt.user.service.StudentService;
import com.ggkt.common.utils.web.RestResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.page.PageMethod;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;



/**
 * <p>
 *  前端控制器
 * </p>
 * 通过pageHelper实现分页
 * @author ggkt
 * @since 2021-05-24
 */
@RestController
@RequestMapping("/studentQ")
@Api(tags = "学生管理RestResult")
public class StudentQueryController {
    @Autowired
    StudentService studentService;
    @Autowired
    StudentMapper studentMapper;

    @ApiOperation("列表方法")
    @PostMapping("/qrList")
    public RestResult qrList(@RequestBody StudentQuery entity){
        Page<StudentQuery> page = PageMethod.startPage(entity.getCurrentPage(),entity.getPageSize());
        studentMapper.getPage(entity);
        return RestResult.success(page);
    }

    @ApiOperation("创建方法")
    @PostMapping("/create")
    public RestResult create(@RequestBody Student entity){
        studentService.saveOrUpdate(entity);
        return RestResult.success();
    }

    @ApiOperation("查看方法")
    @GetMapping("/view/{id}")
    public RestResult view(@PathVariable Integer id){
        if(id==null){
            throw new BaseException("error param!!!");
        }
        Student student =  studentService.getById(id);
        return RestResult.success(student);
    }

    @ApiOperation("删除方法")
    @PostMapping("delete")
    public RestResult delete(@RequestBody Student entity){
        if (entity==null || entity.getId()==null){
            throw new BaseException("error param!!!");
        }else{
            System.out.println("the id is:"+entity.getId());
        }
        boolean result =  studentService.removeById(entity.getId());
        if(result) {
            return RestResult.success();
        }else{
            return RestResult.fail();
        }
    }

}

